package v1

import (
	"context"
	"signal/lib/trace_log"
	"signal/utils"

	base "gitlab.local/backend/proto/base"
	"gitlab.local/backend/proto/dto"
	"signal/bll"
)

type Signal struct{}

func (s *Signal) Reactive(ctx context.Context, in *dto.ReactiveRequest, out *dto.ReactiveReply) error {
	return bll.Command.Reactive(ctx, in)
}

func (s *Signal) FirmwareVersion(ctx context.Context, in *dto.FirmwareVersionRequest, out *dto.FirmwareVersionReply) (err error) {
	var (
		res *dto.FirmwareVersionReply
	)
	trace_log.CommandLogger(in.Sn, "---- FirmwareVersion in", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "---- FirmwareVersion resp:", utils.ToJsonString(out), "err:", err)
	}()

	res, err = bll.Request.FirmwareVersion(ctx, in)
	if err != nil {
		return err
	}
	*out = *res
	return nil
}

func (s *Signal) Controller(ctx context.Context, in *dto.ControllerRequest, out *dto.ControllerReply) (err error) {
	var (
		res *dto.ControllerReply
	)
	trace_log.CommandLogger(in.Sn, "---- Controller in", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "---- Controller resp:", utils.ToJsonString(out), "err:", err)
	}()

	res, err = bll.Request.Controller(ctx, in)
	if err != nil {
		return err
	}
	*out = *res
	return nil
}

func (s *Signal) Upgrade(ctx context.Context, in *dto.UpgradeRequest, empty *base.Empty) error {
	var err error
	trace_log.CommandLogger(in.Sn, "---- Request OTA Upgrade in:", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "----Response OTA Upgrade err", err)
	}()
	err = bll.Upgrade.OTA(ctx, in)
	return err
}

func (s *Signal) UpgradeBSC22(ctx context.Context, in *dto.UpgradeBSC22Data, empty *base.Empty) error {
	var err error
	trace_log.CommandLogger(in.Sn, "---- Request OTA UpgradeBSC22 in:", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "----Response OTA UpgradeBSC22 err", err)
	}()
	err = bll.Upgrade.OTABSC22(ctx, in)
	return err
}

func (s *Signal) BinaryReq(ctx context.Context, in *dto.BinaryRequest, out *dto.BinaryRequestReply) error {
	var (
		err error
		res *dto.BinaryRequestReply
	)

	trace_log.CommandLogger(in.Sn, "---- BinaryReq:", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "---- BinaryReq resp:", utils.ToJsonString(out), " err:", err)
	}()

	trace_log.DebugLogger(in.Sn, "----  BinaryReq:", utils.ToJsonString(in))
	res, err = bll.Request.BinaryReq(ctx, in)
	if err != nil {
		trace_log.DebugLogger(in.Sn, "----  BinaryReq:", utils.ToJsonString(in), " err:", err)
		return err
	}
	*out = *res
	return nil
}

func (s *Signal) Command(ctx context.Context, in *dto.CommandRequest, out *base.Empty) error {
	var err error
	trace_log.CommandLogger(in.Sn, "---- Command in", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "---- Command err:", err)
	}()

	err = bll.Command.
		Command(ctx, in)
	return err
}

func (s *Signal) Setting(ctx context.Context, in *dto.SettingRequest, out *base.Empty) error {
	var err error
	trace_log.CommandLogger(in.Sn, "---- Setting in", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "---- Setting err:", err)
	}()

	err = bll.Setting.Setting(ctx, in)
	return err
}

func (s *Signal) Switch(ctx context.Context, in *dto.SwitchRequest, out *base.Empty) error {
	var err error
	trace_log.CommandLogger(in.Sn, "---- Switch in", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "---- Switch err:", err)
	}()

	err = bll.Switches.Switch(ctx, in)
	return err
}

func (s *Signal) Battery(ctx context.Context, in *dto.BatteryRequest, out *dto.BatteryReply) error {
	var (
		err error
		res *dto.BatteryReply
	)
	trace_log.CommandLogger(in.Sn, "---- Battery in", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "---- Battery resp:", utils.ToJsonString(out), "err:", err)
	}()

	res, err = bll.Request.Battery(ctx, in)
	if err != nil {
		return err
	}
	*out = *res
	return nil
}

func (s *Signal) Location(ctx context.Context, in *dto.LocationRequest, out *dto.Location) (err error) {
	var (
		res *dto.Location
	)

	trace_log.CommandLogger(in.Sn, "---- Location in", utils.ToJsonString(in))
	defer func() {
		trace_log.CommandLogger(in.Sn, "---- Location resp:", utils.ToJsonString(out), "err:", err)
	}()

	res, err = bll.Request.Location(ctx, in)
	if err != nil {
		return err
	}
	*out = *res
	return nil
}
